﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
namespace NJL.WebManage.Attractions
{
    public partial class EditAttractions :  BasePage
    {
        NJL.BLL.NJL_ATTRACTION bll = new BLL.NJL_ATTRACTION();
        NJL.Model.NJL_ATTRACTION model;
        NJL.BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (WEBSITE_CODE != "1")
                {
                    ddlProvince.Visible = false;  
                }
                DataSet dsProvince1 = bllArea.GetList("AREA_CODE like '%0000'");
                ddlProvince.DataSource = dsProvince1;
                ddlProvince.DataTextField = "AREA_NAME";
                ddlProvince.DataValueField = "AREA_CODE";
                ddlProvince.DataBind();
                model = bll.GetModel(Request.QueryString["id"].ToString());
                if (model != null)
                {
                    txtName.Text= model.ATTRACTION_NAME;  //名称
                    string cityId = model.AREA_CODE.Substring(0, 2);
                    DataSet dsCity = bllArea.GetList("AREA_CODE like '" + cityId + "%00' and deep=2");
                    ddlCity.DataSource = dsCity;
                    ddlCity.DataTextField = "AREA_NAME";
                    ddlCity.DataValueField = "AREA_CODE";
                    ddlCity.DataBind();
                    ddlCity.Items.FindByValue(model.AREA_CODE).Selected = true;
                    string province = model.AREA_CODE.Substring(0, 2);
                    DataSet dsProvince = bllArea.GetList("AREA_CODE like '" + province + "0000'");
                    ddlProvince.Items.FindByValue(dsProvince.Tables[0].Rows[0]["AREA_CODE"].ToString()).Selected = true;
                    txtAddress.Text=model.ATTRACTION_ADDRESS;    //地址
                    txtTag.Text=model.ATTRACTION_TAG ;            //标签
                    rbtnLevel.Items.Add(new ListItem("1", "1"));
                    rbtnLevel.Items.Add(new ListItem("2", "2"));
                    rbtnLevel.Items.Add(new ListItem("3", "3"));
                    rbtnLevel.Items.Add(new ListItem("4", "4"));
                    rbtnLevel.Items.Add(new ListItem("5", "5"));       
                    rbtnLevel.Items.FindByValue(model.LEVEL.ToString()).Selected = true;           //级别
                    txtPrice.Text = string.Format("{0:0.00}",model.LOCAL_PRICE);             //本站价格
                    txtSupPrice.Text =string.Format("{0:0.00}",model.MARKET_PRICE);              //市场价格
                    txtSort.Text = model.SORT.ToString();
                    //cbxLpayment.Items.Add(new ListItem("网上支付", "网上支付"));
                    //cbxLpayment.Items.Add(new ListItem("景区现付", "景区现付"));
                    //string[] cbx = model.PAY_TYPE.Split(',');
                    //foreach (string id in cbx)
                    //{
                    //    cbxLpayment.Items.FindByValue(id).Selected = true;       
                    //}
                    //cbxLTheme.Items.Add(new ListItem("爬山", "爬山"));
                    //cbxLTheme.Items.Add(new ListItem("滑雪", "滑雪"));
                    //cbxLTheme.Items.Add(new ListItem("划船", "划船"));
                    //string[] theme = model.VISIT_THEME.Split(',');
                    //foreach (string id in theme)
                    //{   
                    //    cbxLTheme.Items.FindByValue(id).Selected = true;
                    //}
                    ////游客印象
                    //cbxLimpression.Items.Add(new ListItem("一般", "一般"));
                    //cbxLimpression.Items.Add(new ListItem("好", "好"));
                    //cbxLimpression.Items.Add(new ListItem("非常好", "非常好"));
                    //string[] pression = model.TOURIST_IMPRESSION.Split(',');
                    //foreach (string id in pression)
                    //{
                    //    cbxLimpression.Items.FindByValue(id).Selected = true;
                    //}
                    txtTicketNotice.Text=model.TICKET_NOTICE;                                 //购票须知
                    //txtDesc.Text=model.MANUAL_DESCRIPTION;                              //旅游指南介绍
                    //txtTraffic.Text=model.MANUAL_TRAFFIC;                               //旅游指南交通
                    //txtFood.Text=model.MANUAL_FOOD;                                     //美食
                    //txtBuy.Text=model.MANUAL_SHOP;                                      //购物
                    //txtOther.Text=model.MANUAL_OTHER;                                   //其他
                    txtDescription.Text = model.SEO_DESCRIPTION;
                    txtKeyWords.Text = model.SEO_KEYWORDS;
                    txtTitle.Text = model.SEO_TITLE;
                }
                if (model.IMAGE_CODE != "")
                {
                    Image1.ImageUrl = GetImageUrl(model.IMAGE_CODE);
                }
                else
                {
                    Image1.Visible = false;
                }
            
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {        
            model = bll.GetModel(Request.QueryString["id"].ToString());
            model.ATTRACTION_NAME = txtName.Text;  //名称
            model.AREA_CODE = ddlCity.SelectedValue;//地区编号
            model.ATTRACTION_ADDRESS = txtAddress.Text;    //地址
            model.ATTRACTION_TAG = txtTag.Text;            //标签
            model.LEVEL = Convert.ToInt32(rbtnLevel.SelectedValue);          //级别
            model.LOCAL_PRICE = Convert.ToDecimal(txtPrice.Text);                  //本站价格
            model.MARKET_PRICE = Convert.ToDecimal(txtSupPrice.Text);              //市场价格
            model.SORT = int.Parse(txtSort.Text.Trim());
            //string payment = "";
            //foreach (ListItem lit in cbxLpayment.Items)
            //{
            //    if (lit.Selected == true)
            //    {
            //        payment += lit.Value + ",";
            //    }
            //}
            //model.PAY_TYPE = payment.Substring(0, payment.Length - 1);                           //支付方式
            //string theme = "";
            //foreach (ListItem lit in cbxLTheme.Items)
            //{
            //    if (lit.Selected == true)
            //    {
            //        theme += lit.Value + ",";
            //    }
            //}
            //model.VISIT_THEME = theme.Substring(0, theme.Length - 1);                         //游玩主题
            //string pression = string.Empty;
            //foreach (ListItem lit in cbxLimpression.Items)
            //{
            //    if (lit.Selected == true)
            //    {
            //        pression += lit.Value + ",";
            //    }
            //}
            //model.TOURIST_IMPRESSION = pression.Substring(0, pression.Length - 1);             //游客印象
            model.TICKET_NOTICE = txtTicketNotice.Text;                               //购票须知
            //model.MANUAL_DESCRIPTION = txtDesc.Text;                              //旅游指南介绍
            //model.MANUAL_TRAFFIC = txtTraffic.Text;                               //旅游指南交通
            //model.MANUAL_FOOD = txtFood.Text;                                     //美食
            //model.MANUAL_SHOP = txtBuy.Text;                                      //购物
            //model.MANUAL_OTHER = txtOther.Text;                                   //其他
            model.SEO_DESCRIPTION = txtDescription.Text;
            model.SEO_KEYWORDS = txtKeyWords.Text;
            model.SEO_TITLE = txtTitle.Text;
            model.ID = Request.QueryString["id"].ToString();
            model.UPDATE_DATE_TIME = DateTime.Now;
            model.UPDATE_USER_NAME = USER_NAME;
            model.UPDATE_USER_ID = USER_ID;
            if (bll.Update(model))
            {
                #region 标签处理
                NJL.BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
                NJL.Model.NJL_CS_TAG modelTag = new Model.NJL_CS_TAG();
                char[] tagtxt1 = { ',', '，' };
                string[] tag = txtTag.Text.Trim().Split(tagtxt1);
                foreach (string strtag in tag)
                {
                    DataSet ds = bllTag.GetList("TAG='" + strtag + "' and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        modelTag = bllTag.GetModel(ds.Tables[0].Rows[0][0].ToString());
                        if (modelTag != null)
                        {
                            if (modelTag.USED_COUNT != null)
                            {
                                modelTag.USED_COUNT += 1;
                                if (bllTag.Update(modelTag))
                                {
                                }
                            }
                        }
                    }
                    else
                    {
                        if (strtag != "")
                        {
                            modelTag.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random(Guid.NewGuid().GetHashCode()).Next(100, 999).ToString();
                            modelTag.IS_SYS_TAG = 0;
                            modelTag.TAG = strtag;
                            modelTag.CREATE_DATE_TIME = DateTime.Now;
                            modelTag.NJL_SYS_BASE_ID = WEBSITE_CODE;
                            modelTag.SEARCH_COUNT = 0;
                            modelTag.TAG_TYPE = "1";  //景点是1
                            modelTag.USED_COUNT = 0;
                            modelTag.AREA_CODE = ddlCity.SelectedValue;
                            bllTag.Add(modelTag);
                        }
                    }
                }
                #endregion 
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", "EditAttractions.aspx?id=" + Request.QueryString["id"]+"&QQ="+Request.QueryString["QQ"]);
            }
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ProvinceId = ddlProvince.SelectedValue.Substring(0, 2);
            DataSet ds = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
            ddlCity.DataSource = ds;
            ddlCity.DataTextField = "AREA_NAME";
            ddlCity.DataValueField = "AREA_CODE";
            ddlCity.DataBind();
        }
        protected void btnUpload_Click(object sender,EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                model = bll.GetModel(Request.QueryString["id"].ToString());
                if (model != null)
                {
                    DeleteImageByCode(model.IMAGE_CODE);   //删除文件
                    model.IMAGE_CODE = SaveUploadFile(Request.Files);
                    if (bll.Update(model))
                     {
                        Royalsoft.Common.MessageBox.Show(this, "上传成功");
                        if (model.IMAGE_CODE != "")
                        {
                            Image1.ImageUrl = GetImageUrl(model.IMAGE_CODE);
                            Image1.Visible = true;
                        }
                        else
                        {
                            Image1.Visible = false;
                        }
                     }
                  
                }
            }
        }     
    }
}